<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
use cmf\controller\HomeBaseController;
use think\Db;
use think\Request;

class HotController extends HomeBaseController
{
    public function hot(Request $request){
        if($request->isAjax()){
            /*     //$name = $_POST ['map'];
     if(input('grade')!=""){
         $map['grade']=['eq',input('grade')];
     }
                 if(input('subject')!=""){
         $map['subject']=['eq',input('subject')];
     }
                 if(input('test')!=""){
         $map['test']=['eq',input('test')];
     }
                 //3
                $re=Db::name("test")
                     ->where($map)
                     ->select();
                 return json($re);*/
            /* $sub=$request->param('sub');*/
            $sort=$request->param("sort");
            $grade=$request->param("grade");
            $city=$request->param("city");
            $screensex=$request->param("screensex");
            $screenedu=$request->param("screenedu");
            $page=$request->param('page');
            $or="id";
            if(!$page){
                $page=1;
            }
            if($sort){
                if(trim($sort)=="价格最高"){
                    $or='price';
                }
                $re=Db::name("teacher")->join('qq_teacher_experience','qq_teacher.id=qq_teacher_experience.teacher_id')
                    ->join('qq_teacher_course_manager','qq_teacher.id=qq_teacher_course_manager.teacher_id')
                    ->field('qq_teacher.id,qq_teacher.name,qq_teacher.school_age,qq_teacher.city,
                    qq_teacher.sex,qq_teacher.teach_time,qq_teacher_course_manager.subject,
                    qq_teacher_course_manager.grade,qq_teacher_course_manager.price')
                    ->page($page,10)
                    ->order($or, 'desc')
                    ->select();
            }
            else if($grade){
                $re=Db::name("teacher")->join('qq_teacher_experience','qq_teacher.id=qq_teacher_experience.teacher_id')
                    ->join('qq_teacher_course_manager','qq_teacher.id=qq_teacher_course_manager.teacher_id')
                    ->field('qq_teacher.id,qq_teacher.name,qq_teacher.school_age,qq_teacher.city,
                    qq_teacher.sex,qq_teacher.teach_time,qq_teacher_course_manager.subject,
                    qq_teacher_course_manager.grade,qq_teacher_course_manager.price')
                    ->where('qq_teacher_course_manager.grade',$grade)
                    ->page($page,10)
                    ->order($or, 'desc')
                    ->select();
            }
            else if($city){
                $re=Db::name("teacher")->join('qq_teacher_experience','qq_teacher.id=qq_teacher_experience.teacher_id')
                    ->join('qq_teacher_course_manager','qq_teacher.id=qq_teacher_course_manager.teacher_id')
                    ->field('qq_teacher.id,qq_teacher.name,qq_teacher.school_age,
                    qq_teacher.city,qq_teacher.sex,qq_teacher.teach_time,
                    qq_teacher_course_manager.subject,qq_teacher_course_manager.grade,
                    qq_teacher_course_manager.price')
                    ->where('qq_teacher.city',$city)
                    ->page($page,10)
                    ->order($or, 'desc')
                    ->select();
            }
            else if($screensex||$screenedu){
                if($screensex&&$screenedu){
                    if (trim($screenedu)=='10年以上'){
                        $re=Db::name("teacher")->join('qq_teacher_experience','qq_teacher.id=qq_teacher_experience.teacher_id')
                            ->join('qq_teacher_course_manager','qq_teacher.id=qq_teacher_course_manager.teacher_id')
                            ->field('qq_teacher.id,qq_teacher.name,qq_teacher.school_age,qq_teacher.city,
                            qq_teacher.sex,qq_teacher.teach_time,qq_teacher_course_manager.subject,
                            qq_teacher_course_manager.grade,qq_teacher_course_manager.price')
                            ->where('qq_teacher.sex',$screensex)
                            ->where('qq_teacher.school_age','>',10)
                            ->page($page,10)
                            ->order($or,'desc')
                            ->select();
                    }
                    elseif(trim($screenedu)=='5年以下'){
                        $re=Db::name("teacher")->join('qq_teacher_experience','qq_teacher.id=qq_teacher_experience.teacher_id')
                            ->join('qq_teacher_course_manager','qq_teacher.id=qq_teacher_course_manager.teacher_id')
                            ->field('qq_teacher.id,qq_teacher.name,qq_teacher.school_age,qq_teacher.city,
                            qq_teacher.sex,qq_teacher.teach_time,qq_teacher_course_manager.subject,
                            qq_teacher_course_manager.grade,qq_teacher_course_manager.price')
                            ->where('qq_teacher.sex',$screensex)
                            ->where('qq_teacher.school_age','<',5)
                            ->page($page,10)
                            ->order($or,'desc')
                            ->select();
                    }
                    else{
                        $re=Db::name("teacher")->join('qq_teacher_experience','qq_teacher.id=qq_teacher_experience.teacher_id')
                            ->join('qq_teacher_course_manager','qq_teacher.id=qq_teacher_course_manager.teacher_id')
                            ->field('qq_teacher.id,qq_teacher.name,qq_teacher.school_age,qq_teacher.city,
                            qq_teacher.sex,qq_teacher.teach_time,qq_teacher_course_manager.subject,
                            qq_teacher_course_manager.grade,qq_teacher_course_manager.price')
                            ->where('qq_teacher.sex',$screensex)
                            ->where('qq_teacher.school_age','between',[5, 10])
                            ->page($page,10)
                            ->order($or,'desc')
                            ->select();
                    }
                }
                if($screensex!=null&&$screenedu==null){
                    $re=Db::name("teacher")->join('qq_teacher_experience','qq_teacher.id=qq_teacher_experience.teacher_id')
                        ->join('qq_teacher_course_manager','qq_teacher.id=qq_teacher_course_manager.teacher_id')
                        ->field('qq_teacher.id,qq_teacher.name,qq_teacher.school_age,qq_teacher.city,
                        qq_teacher.sex,qq_teacher.teach_time,qq_teacher_course_manager.subject,
                        qq_teacher_course_manager.grade,qq_teacher_course_manager.price')
                        ->where('qq_teacher.sex',$screensex)
                        ->page($page,10)
                        ->order('id','desc')
                        ->select();
                }
                if ($screensex==null&&$screenedu!=null){

                    if (trim($screenedu)=='10年以上'){
                        $re=Db::name("teacher")->join('qq_teacher_experience','qq_teacher.id=qq_teacher_experience.teacher_id')
                            ->join('qq_teacher_course_manager','qq_teacher.id=qq_teacher_course_manager.teacher_id')
                            ->field('qq_teacher.id,qq_teacher.name,qq_teacher.school_age,qq_teacher.city,
                            qq_teacher.sex,qq_teacher.teach_time,qq_teacher_course_manager.subject,
                            qq_teacher_course_manager.grade,qq_teacher_course_manager.price')
                            ->where('qq_teacher.school_age','>',10)
                            ->page($page,10)
                            ->order($or,'desc')
                            ->select();
                    }
                    elseif(trim($screenedu)=='5年以下'){
                        $re=Db::name("teacher")->join('qq_teacher_experience','qq_teacher.id=qq_teacher_experience.teacher_id')
                            ->join('qq_teacher_course_manager','qq_teacher.id=qq_teacher_course_manager.teacher_id')
                            ->field('qq_teacher.id,qq_teacher.name,qq_teacher.school_age,qq_teacher.city,
                            qq_teacher.sex,qq_teacher.teach_time,qq_teacher_course_manager.subject,
                            qq_teacher_course_manager.grade,qq_teacher_course_manager.price')
                            ->where('qq_teacher.school_age','<',5)
                            ->page($page,10)
                            ->order($or,'desc')
                            ->select();
                    }
                    else{
                        $re=Db::name("teacher")->join('qq_teacher_experience','qq_teacher.id=qq_teacher_experience.teacher_id')
                            ->join('qq_teacher_course_manager','qq_teacher.id=qq_teacher_course_manager.teacher_id')
                            ->field('qq_teacher.id,qq_teacher.name,qq_teacher.school_age,qq_teacher.city,qq_teacher.sex,qq_teacher.teach_time,qq_teacher_course_manager.subject,qq_teacher_course_manager.grade,qq_teacher_course_manager.price')
                            ->where('qq_teacher_course_manager.subject',0)
                            ->where('qq_teacher.school_age','between',[5, 10])
                            ->page($page,10)
                            ->order($or,'desc')
                            ->select();
                    }
                }
            }
            else{
                $re=Db::name("teacher")->join('qq_teacher_experience','qq_teacher.id=qq_teacher_experience.teacher_id')
                    ->join('qq_teacher_course_manager','qq_teacher.id=qq_teacher_course_manager.teacher_id')
                    ->field('qq_teacher.id,qq_teacher.name,qq_teacher.school_age,qq_teacher.city,qq_teacher.sex,qq_teacher.teach_time,qq_teacher_course_manager.subject,qq_teacher_course_manager.grade,qq_teacher_course_manager.price')
                    ->page($page,10)
                    ->order($or, 'desc')
                    ->select();
            }
            // die(Db::name("teacher").getLastSql());
            return json($re);
        }
        else{
            /*dump("4");
            die;*/
            return $this->fetch(':hot');
        }
    }

}
